package cn.lj.payment.service.service.impl;

import cn.lj.constant.AliPayConstant;
import cn.lj.enums.RestResultEmum;
import cn.lj.enums.RestResult;
import cn.lj.payment.config.redis.RedisServie;
import cn.lj.payment.mapper.mapper.OrderMapper;
import cn.lj.payment.mapper.pojo.SmallMiGoodsOrder;
import cn.lj.payment.service.bo.PayMent;
import cn.lj.payment.service.service.PayMentService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.ijpay.alipay.AliPayApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description: OrderServiceImpl
 * @author: lijie
 * @date: 2021年07月26日 上午 09:55:28
 */
@Service
public class PayMentServiceImpl implements PayMentService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisServie redisServie;
    @Override
    public void alipay(SmallMiGoodsOrder smallMiGoodsOrder, HttpServletResponse response) throws AlipayApiException, IOException {
        AlipayTradePagePayModel payModel = new AlipayTradePagePayModel();
        payModel.setOutTradeNo(smallMiGoodsOrder.getOrder_number());//订单
        payModel.setProductCode("FAST_INSTANT_TRADE_PAY");//产品吗
        payModel.setTotalAmount(smallMiGoodsOrder.getMoney().toString());//金额
        payModel.setSubject("Iphone6 16G");//标题
        AliPayApi.tradePage(response,payModel,"http://4ixp6n.natappfree.cc/orde/getreturn",null);
    }
    /**
     * 功能描述:如果回调成功则返回到这个方法进行判断数据一致
     * @auther: lijie
     * @param map:
     * @date: 2021/6/25 下午 08:17:13
     * @return: cn.util.enums.RestResult<java.lang.Void>
     */
    @Override
    public RestResult<Void> checkNotifyurl(Map<String, String> map) {
        //拿到支付宝返回的通知类型
        String trade = map.get("trade_status");
        //拿到订单号
        String out = map.get("out_trade_no");
        //拿到金额
        String money = map.get("total_amount");
        //把金额转成金额类型
        BigDecimal bigDecimal=new BigDecimal(money);
        //新建实体类赋值
        PayMent payMent = new PayMent();
        payMent.setTradeStatus(trade);
        payMent.setOutTradeNo(out);
        payMent.setTotalAmount(bigDecimal);
        //调用共有方法判断数据和修改数据库
        return publicCheckNotifyurl(payMent);
    }
    /**
     * 功能描述: 回调共有方法，用来判断数据和修改数据库
     * @auther: lijie
     * @param payMent:
     * @date: 2021/6/26 上午 09:35:5
     * @return: void
     */
    @Override
    public RestResult<Void> publicCheckNotifyurl(PayMent payMent){
        //判断支付宝返回的数据是否是支付成功的标识
        if(payMent.getTradeStatus().equals(AliPayConstant.TRADE_SUCCESS)||payMent.getTradeStatus().equals(AliPayConstant.TRADE_FINISHED)){
            //去数据库中查询订单
            SmallMiGoodsOrder one = orderMapper.getOne(payMent.getOutTradeNo());
            //判断订单号是否正确
            if (one == null){
                return new RestResult<>(RestResultEmum.GET_ERROR.code,RestResultEmum.GET_ERROR.msg);
            }
            //判断金额是否正确
            int i = payMent.getTotalAmount().compareTo(one.getMoney());
            //如果为0则是正确的
            if (i == 0){
                //加锁，让两个数据不能同时进入
                if (redisServie.tryLock("order",payMent.getOutTradeNo(),5L)){
                    //修改数据库的支付状态
                    int a = orderMapper.orderUpdate(payMent.getOutTradeNo());
                    //如果修改成功
                    if (a==1){
                        return new RestResult<>(RestResultEmum.SUCCESS.code,RestResultEmum.SUCCESS.msg);
                    }
                    return new RestResult<>(RestResultEmum.GET_ERROR.code,RestResultEmum.GET_ERROR.msg);
                }
                return new RestResult<>(RestResultEmum.GET_ERROR.code,RestResultEmum.GET_ERROR.msg);
            }
        }
        return new RestResult<>(RestResultEmum.GET_ERROR.code,RestResultEmum.GET_ERROR.msg);
    }
    /**
     * 功能描述：如果回调不成功就执行这个方法，用来定时任务调用
     * @auther: lijie
     * @param payMent:
     * @date: 2021/6/26 上午 11:56:5
     * @return: cn.util.enums.RestResult<java.lang.Void>
     */
    @Override
    public RestResult<Void> notcheckNotifyurl(PayMent payMent) {
        return publicCheckNotifyurl(payMent);
    }

    @Override
    public List<SmallMiGoodsOrder> getorderStatus() {
        return orderMapper.getOneStatus();
    }
}
